package com.zhh.dataservice.service;

import com.zhh.api.model.RechargeRecord;
import com.zhh.api.service.RechargeService;
import com.zhh.common.contants.FMContants;
import com.zhh.common.util.CommonUtil;
import com.zhh.dataservice.mapper.FinanceAccountMapper;
import com.zhh.dataservice.mapper.RechargeRecordMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

//用户中心的充值查询
@DubboService(interfaceClass = RechargeService.class,version = "1.0")
public class RechargeServiceImpl implements RechargeService {

    @Resource
    private RechargeRecordMapper rechargeMapper;

    @Resource
    private FinanceAccountMapper accountMapper;

    @Override
    public List<RechargeRecord> queryByUid(Integer uid, Integer pageNo, Integer pageSize) {
        //储存记录
        List<RechargeRecord> records = new ArrayList<>();
        //验证数据
        if(uid != null && uid > 0){
            pageNo = CommonUtil.defaultPageNo(pageNo);
            pageSize = CommonUtil.defaultPageSize(pageSize);
            int offset = (pageNo-1) * pageSize;

            //用分页查询，通过uid查询充值记录
            records = rechargeMapper.selectByUid(uid,offset,pageSize);
        }
        return records;
    }

    //创建充值记录
    @Override
    public int addRechargeRecord(RechargeRecord record) {
        return rechargeMapper.insertSelective(record);
    }

    //处理后续充值
    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized int handleKQNotify(String orderId, String payAmount, String payResult) { //传入参数：订单号，充值金额，充值结果
        int result = 0;
        int rows = 0;
        //1.根据订单号，查找充值记录
        RechargeRecord record = rechargeMapper.selectByRechargeNo(orderId);
        if(record != null){
            //查找到了订单，查看订单状态是不是正在充值中
            if(record.getRechargeStatus() == FMContants.RECHARGE_STATUS_PROCESSING){
                //2.判断充值金额是否正确
                //转换单位为分
                String fen = record.getRechargeMoney().multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString();
                if(fen.equals(payAmount)){
                    //金额一致
                    //3.判断充值是否成功
                    if("10".equals(payResult)) {
                        //充值成功
                        //4.更新用户资金
                        rows = accountMapper.updateAvailableMoneyByRecharge(record.getUid(), record.getRechargeMoney());
                        if (rows < 1) {
                            throw new RuntimeException("资金账号更新失败");
                        }

                        //5.更新充值记录状态，更新为成功 ”1“
                        rechargeMapper.updateStatus(record.getId(), FMContants.RECHARGE_STATUS_SUCCESS);
                        if (rows < 1) {
                            throw new RuntimeException("充值记录状态更新失败");
                        }
                        result = 1; //成功

                    }else{
                        //更新充值记录状态，更新为失败 ”2“
                        rechargeMapper.updateStatus(record.getId(), FMContants.RECHARGE_STATUS_FAIL);
                        if (rows < 1) {
                            throw new RuntimeException("充值记录状态更新失败");
                        }
                        result = 2; //充值失败
                    }
                }else{
                    result = 4; //金额不一致
                }
            }else{
                result = 3; //订单已经处理过了

            }
        }else{

        }





        return result;
    }
}
